/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.facades.business;

import com.sdd.entities.Address;
import com.sdd.entities.Customer;
import com.sdd.entities.Delivery;
import com.sdd.entities.Orderr;
import com.sdd.entities.OrderItem;
import com.sdd.entities.Product;
import com.sdd.facades.persistence.OrderItemPersistenceFacade;
import com.sdd.facades.persistence.OrderPersistenceFacade;
import com.sdd.plains.AddressPlain;
import com.sdd.plains.CartItem;
import com.sdd.plains.CartPlain;
import com.sdd.plains.CustomerPlain;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.criteria.Order;

/**
 *
 * @author Fernando
 */
@Stateless
@LocalBean
public class OrderBusinessFacadeBean {
    @EJB
    OrderPersistenceFacade orderPersistenceFacade;
    @EJB
    OrderItemPersistenceFacade orderItemPersistenceFacade;
    @EJB
    CustomerBusinessFacadeBean customerBusinessFacadeBean;
    @EJB
    ProductBusinessFacadeBean productBusinessFacadeBean;
    @EJB
    DeliveryBusinessFacadeBean deliveryBusinessFacadeBean;
    

    public OrderBusinessFacadeBean(){
    }

     public Orderr getOrderr(Long id){
        return orderPersistenceFacade.find(id);
    }
    
    public Orderr addOrder(CartPlain cartPlain, CustomerPlain customerPlain, AddressPlain addressPlain) {
        Orderr order = new Orderr();
        Address addressEntity = new Address();
        addressEntity.setCity(addressPlain.getCity());
        addressEntity.setCountry(addressPlain.getCountry());
        addressEntity.setNumber(addressPlain.getNumber());
        addressEntity.setPostCode(addressPlain.getPostCode());
        addressEntity.setStreet(addressPlain.getStreet());
        addressEntity.setId(addressPlain.getId());
        order.setAddress(addressEntity);
        Customer customer = customerBusinessFacadeBean.getCustomer(customerPlain.getId());
        order.setCustomer(customer);
        order.setStatus(Orderr.SAVED);
        order.setDateAdded(new Date());
        Iterator<CartItem> it = cartPlain.iterator();
        CartItem cartItem;
        ArrayList<OrderItem> al = new ArrayList<OrderItem>();
        order.setOrderItems(al);
        while(it.hasNext()) {
            cartItem = it.next();
            OrderItem orderItem = new OrderItem();
            Product product = productBusinessFacadeBean.getProduct(cartItem.getProduct().getProductId());
            orderItem.setOrder(order);
            orderItem.setProduct(product);
            orderItem.setQty(cartItem.getQuantity());
            OrderItem oc = orderItemPersistenceFacade.create(orderItem);
            al.add(oc);
        }
        return orderPersistenceFacade.create(order);
    }

    public Orderr updateOrderPaid(Long idOrder){
        Orderr order = getOrderr(idOrder);
        order.setStatus(Orderr.PAID);
        return(orderPersistenceFacade.edit(order));
        
        
    }
    public Orderr updateOrder(Long idOrder, Long idDelivery) {
        Delivery delivery = deliveryBusinessFacadeBean.getDelivery(idDelivery);
        Orderr order = getOrderr(idOrder);
        order.setStatus(Orderr.READY);
        order.setDelivery(delivery);
        return(orderPersistenceFacade.edit(order));
    }
}
